package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户订单表操作接口实现类
 * Created by 王成 on 2020/2/6
 */
public class UserOrderInfoMapperImpl implements UserOrderInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public UserOrderInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertUserOrderInfo(UserOrderInfo userOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertUserOrderInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, userOrderInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记用户订单信息失败,instId:<{}>,msgSrcId:<{}>,mchntOrderId:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>", userOrderInfo.getInstId(), userOrderInfo.getMsgSrcId(), userOrderInfo.getMchntOrderId(), userOrderInfo.getSysSeqNum(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateUserOrderInfoByPrimaryKey(UserOrderInfo userOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateUserOrderInfoByPrimaryKey");
        userOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, userOrderInfo, null);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateUserOrderInfoHisByPrimaryKey");
            iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, userOrderInfo, null);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("根据主键更新用户订单信息失败,instId:<{}>,sysSeqNum:<{}>,mchntOrderId:<{}>,iReturnCode:<{}>", userOrderInfo.getInstId(), userOrderInfo.getSysSeqNum(), userOrderInfo.getMchntOrderId(), iReturnCode);
            }
        }
        return iReturnCode;
    }

    @Override
    public int updateUserOrderInfoByUserOrderId(UserOrderInfo userOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateUserOrderInfoByUserOrderId");
        userOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, userOrderInfo, null);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateUserOrderInfoHisByUserOrderId");
            iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, userOrderInfo, null);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("根据用户订单号更新用户订单信息失败,instId:<{}>,msgSrcId:<{}>,userOrderId:<{}>,iReturnCode:<{}>", userOrderInfo.getInstId(), userOrderInfo.getMsgSrcId(), userOrderInfo.getUserOrderId(), iReturnCode);
            }
        }
        return iReturnCode;
    }

    @Override
    public UserOrderInfo selectUserOrderInfoByUserOrderId(String instId, String userOrderId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserOrderInfoByUserOrderId");
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        userOrderInfo.setInstId(instId);
        userOrderInfo.setUserOrderId(userOrderId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userOrderInfo, userOrderInfo);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserOrderInfoHisByUserOrderId");
            iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userOrderInfo, null);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.warn("根据用户订单号查询用户订单信息失败,instId:<{}>,userOrderId:<{}>,iReturnCode:<{}>", instId, userOrderId, iReturnCode);
                return null;
            }
        }
        return userOrderInfo;
    }

    @Override
    public UserOrderInfo selectUserOrderInfoByMchntOrderId(String instId, String mchntOrderId, String msgSrcId, String transChannelId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserOrderInfoByMchntOrderId");
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        userOrderInfo.setInstId(instId);
        userOrderInfo.setMchntOrderId(mchntOrderId);
        userOrderInfo.setMsgSrcId(msgSrcId);
        userOrderInfo.setTransChannelId(transChannelId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userOrderInfo, userOrderInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.warn("根据商户订单号查询用户订单信息失败,instId:<{}>,mchntOrderId:<{}>,msgSrcId:<{}>,transChannelId:<{}>,iReturnCode:<{}>", instId, mchntOrderId, msgSrcId, transChannelId, iReturnCode);
            return null;
        }
        return userOrderInfo;
    }

    @Override
    public UserOrderInfo selectUserOrderInfoByFrontSeqNum(String instId, String frontSeqNum, String msgSrcId, String transChannelId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserOrderInfoByFrontSeqNum");
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        userOrderInfo.setInstId(instId);
        userOrderInfo.setFrontSeqNum(frontSeqNum);
        userOrderInfo.setMsgSrcId(msgSrcId);
        userOrderInfo.setTransChannelId(transChannelId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userOrderInfo, userOrderInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.warn("根据前端流水号查询用户订单信息失败,instId:<{}>,frontSeqNum:<{}>,msgSrcId:<{}>,transChannelId:<{}>,iReturnCode:<{}>", instId, frontSeqNum, msgSrcId, transChannelId, iReturnCode);
            return null;
        }
        return userOrderInfo;
    }

    @Override
    public UserOrderInfo selectUserOrderInfoByPrimaryKey(String instId, String sysSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserOrderInfoByPrimaryKey");
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        userOrderInfo.setInstId(instId);
        userOrderInfo.setSysSeqNum(sysSeqNum);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userOrderInfo, userOrderInfo);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserOrderInfoHisByPrimaryKey");
            iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userOrderInfo, userOrderInfo);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.warn("根据系统流水号查询用户订单信息失败,instId:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>", instId, sysSeqNum, iReturnCode);
                return null;
            }
        }
        return userOrderInfo;
    }

    @Override
    public UserOrderInfo selectUserOrderInfoByOrderUrl(String instId, String orderUrl) throws Exception {
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(orderUrl)) {
            rglog.error("多法人标识<{}>及订单URL<{}>不能为空!", instId, orderUrl);
            return null;
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserOrderInfoByOrderUrl");
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        userOrderInfo.setInstId(instId);
        userOrderInfo.setOrderUrl(orderUrl);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, userOrderInfo, userOrderInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.warn("根据订单号查询用户订单信息失败,instId:<{}>,orderUrl:<{}>,iReturnCode:<{}>", instId, orderUrl, iReturnCode);
            return null;
        }
        return userOrderInfo;
    }

    @Override
    public List<UserOrderInfo> selectUserOrderInfoByOrigSysSeqNum(String instId, String origSysSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserOrderInfoByOrigSysSeqNum");
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        userOrderInfo.setInstId(instId);
        userOrderInfo.setOrigSysSeqNum(origSysSeqNum);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, userOrderInfo, UserOrderInfo.class);
        if (null != objectList && objectList.isEmpty()) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserOrderInfoHisByOrigSysSeqNum");
            objectList = dbsUtil.dbsFetchLimitListData(sqlStr, userOrderInfo, UserOrderInfo.class);
            if (null == objectList || objectList.isEmpty()) {
                rglog.warn("根据原交易流水号查询用户订单信息失败,instId:<{}>,origSysSeqNum:<{}>", instId, origSysSeqNum);
                return null;
            }
        } else if (null == objectList) {
            rglog.warn("根据原交易流水号查询用户订单信息失败,instId:<{}>,origSysSeqNum:<{}>", instId, origSysSeqNum);
            return null;
        }
        return objectList.stream().map(item -> (UserOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectUserOrderInfoByUserId(String instId, String userId, String beginDate, String endDate, PageInfo pageInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserOrderInfoByUserId");
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        userOrderInfo.setInstId(instId);
        userOrderInfo.setUserId(userId);
        userOrderInfo.setTxnDate(beginDate);
        userOrderInfo.setRemark1(endDate);
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, userOrderInfo, UserOrderInfo.class, pageInfo);
        if (null == pageResult || null == pageResult.getResult() || pageResult.getResult().isEmpty()) {
            rglog.warn("根据用户标识查询用户订单信息失败,instId:<{}>,userId:<{}>,beginDate:<{}>,endDate:<{}>", instId, userId, beginDate, endDate);
            return null;
        }
        return pageResult;
    }
}
